package cc.eddic.pratice.programmingparadigms.streampractice.impl;

import cc.eddic.pratice.programmingparadigms.emp02list.impl.ResultListGcsImpl;
import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Option;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.impl.MasterDataServiceLxImpl;
import cc.eddic.pratice.programmingparadigms.streampractice.QuestionAccuracy;
import cc.eddic.pratice.programmingparadigms.streampractice.QuestionAndAnswer;
import cc.eddic.pratice.programmingparadigms.streampractice.ScoreOfStudentAndCategory;
import cc.eddic.pratice.programmingparadigms.streampractice.StreamPractice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamPracticeLxImpl implements StreamPractice {
    private static final Logger log = LoggerFactory.getLogger(ResultListGcsImpl.class);
    MasterDataServiceLxImpl masterDataServiceLx = new MasterDataServiceLxImpl();

    @Override
    public List<QuestionAndAnswer> mapToQuestionAndAnswer(List<Question> questionList) {
//        1.题号2.题目3.正确选项内容4.正确答案
        List<QuestionAndAnswer> questionAndAnswerList = new ArrayList<>();
        for (Question q : questionList) {
            List<Option> optionList = new ArrayList<>();
            for (Option o : q.getOptions()) {
                if (o.isCorrectAnswer()) {
                    questionAndAnswerList.add(new QuestionAndAnswer(q.getNo(), q.getDescription(), o.getChoice(), o.getAnswer()));
                    if (log.isDebugEnabled()) log.debug("正确选项: {},正确内容", o.getChoice(), o.getAnswer());
                }
            }
        }
        return questionAndAnswerList;
    }

    /**
     * 1.拿到所有包含正确答案的question，统计answerlist中答题的学生总数
     * 2.统计一道题有多少学生答对
     * （1）拿到学生答题表importAnswer
     * （2）统计每道题答对的学生数
     * 3.计算每道题的正确率：选项答对的学生总数/答题学生总数
     */
    public List<QuestionAccuracy> calculateAccuracy(List<Answer> answerList) {
        Map<String, Integer> studengCount = new HashMap<>();
        for (Answer a : answerList) {
//            统计answerlist中答题的学生总数
            if (studengCount.containsKey(a.getStudent().getName())) {
                studengCount.put(a.getStudent().getName(), studengCount.get(a.getStudent().getName()) + 1);
            } else {
                studengCount.put(a.getStudent().getName(), 1);
            }
        }

        Map<Question, Integer> answerMap = new HashMap<>();
        for (Answer a : answerList) {
            //            统计一道题有多少学生答对
            List<Option> optionList = a.getQuestion().getOptions();
            for (Option o : optionList) {
                if (o.isCorrectAnswer()) {
                    if (o.getChoice() == a.getChoice()) {
                        if (log.isDebugEnabled()) log.debug("正确选项:{}", o.getChoice());
                        if (answerMap.containsKey(a.getQuestion())) {
                            answerMap.put(a.getQuestion(), answerMap.get(a.getQuestion()) + 1);
                            if (log.isDebugEnabled()) log.debug("复杂方法:{}", answerMap.get(a.getQuestion()));
                        } else {
                            answerMap.put(a.getQuestion(), 1);
                            if (log.isDebugEnabled()) log.debug("复杂方法2:{}", answerMap.get(a.getQuestion()));
                        }
                    }
                }
            }
        }
        int sCount = studengCount.size();
        List<QuestionAccuracy> questionAccuracies = new ArrayList<>();
        for (Question q : answerMap.keySet()) {
            int a = answerMap.get(q);
            double accuracy = (double) a;
            QuestionAccuracy questionAccuracy = new QuestionAccuracy(q.getNo(), q.getDescription(), accuracy / sCount);
            if (log.isDebugEnabled())
                log.debug("复杂方法正确率: {}:{}", questionAccuracy.getQuestion(), questionAccuracy.getAccuracy());
            questionAccuracies.add(questionAccuracy);
        }
        return questionAccuracies;
    }

    public List<ScoreOfStudentAndCategory> calculateScoreOfStudentAndSubCategory(List<Answer> answerList) {
        return answerList.stream().filter(
//                首先判断所选选项和正确选项
                answer -> answer.getChoice() == answer.getQuestion().getOptions()
                        .stream().filter(Option::isCorrectAnswer).findFirst()
                        .orElse(new Option("", '*', true)).getChoice())
                .collect(Collectors.groupingBy(answer -> answer.getStudent().getName()
                        , Collectors.groupingBy(answer -> answer.getQuestion().getSubCategory().getName(), Collectors.counting())))
                .entrySet().stream()
                .flatMap(stu -> stu.getValue().entrySet().stream()
                        .map(category -> new ScoreOfStudentAndCategory(stu.getKey(), category.getKey(), category.getValue().intValue() * 4))).collect(Collectors.toList());
    }
}
